﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary;

namespace Finance
{
    public partial class MainForm : MainFrame 
    {
        public MainForm()
        {
            InitializeComponent();
            this.mainHeader.OnBackToMainView += new DoSampleEvent(OnBackToMainView);
        }

        /// <summary>
        /// 回到主窗口
        /// </summary>
        public override void OnBackToMainView()
        {
            this.LoadView(typeof(MainView));
        }

        /// <summary>
        /// 加载用户控件
        /// </summary>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public override BaseView LoadView(Type targetType)
        {
            BaseView newView = null;
            try
            {
                foreach (Control control in this.MainPanel.Controls)
                {
                    if (control.GetType() == targetType)
                    {
                        newView = (BaseView)control;
                        break;
                    }
                }
                if (newView == null)
                {
                    newView = (BaseView)Activator.CreateInstance(targetType);
                    MainPanel.Controls.Add(newView);
                }
                if (m_currentView != newView)
                {
                    newView.ParentForm = this;
                    newView.Top = newView.Left = 0;
                    newView.Width = MainPanel.ClientSize.Width;
                    newView.Width = MainPanel.ClientSize.Width;
                    newView.Dock = DockStyle.Fill;
                    newView.Visible = true;
                    newView.Focus();
                    newView.OnLoad();
                    if (m_currentView != null)
                    {
                        m_currentView.Visible = false;
                        m_currentView.Hide();
                    }
                    m_currentView = newView;
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return newView;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.LoadView(typeof(MainView));
        }
    }
}
